package com.aicheyoudao;

import android.app.Activity;
import android.app.Application;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import com.aicheyoudao.utils.HistoryDB;
import com.baidu.lbsapi.auth.LBSAuthManagerListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;
import com.baidu.navisdk.BaiduNaviManager;
import com.baidu.navisdk.BNaviEngineManager.NaviEngineInitListener;

public class MyApplication extends Application {

	private BDLocation mlocation;
	private static MyApplication instance;
	private locationFinishListener locationlis;
	public LocationClient mLocClient;
	private HistoryDB db;
	private boolean mIsEngineInitSuccess;

	@Override
	public void onCreate() {
		super.onCreate();
		initLocation();
		instance = this;
		db = new HistoryDB(this);

	}

	public static MyApplication getInstance() {
		return instance;
	}

	public BDLocation getMlocation() {
		return mlocation;
	}

	public LocationClient getmLocClient() {
		return mLocClient;
	}

	public HistoryDB getDb() {
		return db;
	}

	public boolean ismIsEngineInitSuccess() {
		return mIsEngineInitSuccess;
	}

	/**
	 * 初始化定位的相关设置
	 */
	private void initLocation() {
		// mCurrentMode = LocationMode.NORMAL;
		// 传入null则，恢复默认图标
		// mCurrentMarker = null;

		// 定位初始化
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(new MyLocationListenner());
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(5000);// /设置发起定位请求的间隔时间为5000ms
		option.setIsNeedAddress(true);
		// 定位模式:高精定
		option.setLocationMode(com.baidu.location.LocationClientOption.LocationMode.Hight_Accuracy);
		mLocClient.setLocOption(option);
		// mBaiduMap.setMyLocationConfigeration(new MyLocationConfigeration(
		// LocationMode.NORMAL, true, mCurrentMarker));
		mLocClient.start();
	}

	public void initNavigation(Activity activity) {
		BaiduNaviManager.getInstance().initEngine(activity, getSdcardDir(), mNaviEngineInitListener, new LBSAuthManagerListener() {
			@Override
			public void onAuthResult(int status, String msg) {
				String str = null;
				if (0 == status) {
					str = "key校验成功!";
				} else {
					str = "key校验失败, " + msg;
				}
				Toast.makeText(MyApplication.this, str, Toast.LENGTH_LONG).show();
			}
		});

	}

	private String getSdcardDir() {
		if (Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) {
			return Environment.getExternalStorageDirectory().toString();
		}
		return null;
	}

	private NaviEngineInitListener mNaviEngineInitListener = new NaviEngineInitListener() {
		public void engineInitSuccess() {
			mIsEngineInitSuccess = true;
			Log.i("info", "导航初始化成功");
		}

		public void engineInitStart() {
			Log.i("info", "导航开始初始化");
		}

		public void engineInitFail() {
			Log.i("info", "导航初始化失败");
		}
	};

	/**
	 * 定位SDK监听函数
	 */
	public class MyLocationListenner implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location != null) {
				mlocation = location;
				if (locationlis != null) {
					Log.i("info", "定位成功==" + location.getCity());
					locationlis.ReceiveLocation(location);
				}
			}
		}
	}

	public void setonlocationFinishListener(locationFinishListener lis) {
		this.locationlis = lis;
	}

	public interface locationFinishListener {
		public void ReceiveLocation(BDLocation location);
	}

}
